
package com.tools.cleanmaster.store;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

public class GlobalSharedPrefManager {

    private static final String PREFS_FILE_DEFALT = "prefs_file_default";
    private static final String PREFS_KEY_FIRST_ACTIVE_TIME = "active_time";
    private static final String PREFS_KEY_SPLASH_PROTECT_FIRST_ACTIVE_TIME = "splash_protect_active_time";
    private static final String PREFS_KEY_CHECK_UPDATE_WAKEUP_TIME = "checkupdate_wakeup";
    private static SharedPreferences sPrefs = null;

    //用户来源
    public static final int USER_FROM_ORGANIC = 1;//organic新增
    public static final int USER_FROM_RECOMMEND = 2;//带量新增
    public static final int USER_FROM_FACEBOOK_PURCHASE = 3;//facebook新增
    public static final int USER_FROM_OTHER_WEBUNION_PURCHASE = 4;//其它网盟买量
    private static final String USER_SOURCE_TAG = "user_source";

    private static SharedPreferences initSharedPreferences(Context ctx) {
        if (sPrefs == null) {
            sPrefs = ctx.getSharedPreferences(PREFS_FILE_DEFALT, Context.MODE_PRIVATE);
        }
        return sPrefs;
    }

    public static long getFirstActiveTime(Context ctx) {
        SharedPreferences prefs = initSharedPreferences(ctx);
        return prefs.getLong(PREFS_KEY_FIRST_ACTIVE_TIME, System.currentTimeMillis());
    }

    public static void setFirstActiveTime(Context ctx, long activeTime) {
        SharedPreferences prefs = initSharedPreferences(ctx);
        if (!prefs.contains(PREFS_KEY_FIRST_ACTIVE_TIME)) {
            prefs.edit().putLong(PREFS_KEY_FIRST_ACTIVE_TIME,
                    activeTime).apply();
        }
    }

    /**
     * 获取加入"新用户保护时间"需求后, 闪屏用户的活跃时间起点
     * 1.4.0版本以前的用户以升级到1.4.0版本为起点, 新用户则采用第一次启动时间
     */
    public static long getSplashProtectFirstActiveTime(Context ctx) {
        SharedPreferences prefs = initSharedPreferences(ctx);
        return prefs.getLong(PREFS_KEY_SPLASH_PROTECT_FIRST_ACTIVE_TIME, System.currentTimeMillis());
    }

    public static void setSplashProtectFirstActiveTime(Context ctx, long activeTime) {
        SharedPreferences prefs = initSharedPreferences(ctx);
        if (!prefs.contains(PREFS_KEY_SPLASH_PROTECT_FIRST_ACTIVE_TIME)) {
            prefs.edit().putLong(PREFS_KEY_SPLASH_PROTECT_FIRST_ACTIVE_TIME, activeTime).apply();
        }
    }

    public static long getCheckUpdateWakeupTime(Context ctx) {
        SharedPreferences prefs = initSharedPreferences(ctx);
        return prefs.getLong(PREFS_KEY_CHECK_UPDATE_WAKEUP_TIME, 0);
    }

    public static void setCheckUpdateWakeupTime(Context ctx, long activeTime) {
        SharedPreferences prefs = initSharedPreferences(ctx);
        prefs.edit().putLong(PREFS_KEY_CHECK_UPDATE_WAKEUP_TIME, activeTime).apply();
    }

    /**
     * 获取用户来源（organic,带量,facebook买量,其它网盟买量)
     */
    public static int getUserSource(Context context) {
        if (null != context) {
            if (!TextUtils.isEmpty(GlobalConfigSharedPrefManager.getFacebookDeeplink(context))) {
                if (FeatureConfig.DEBUG_LOG) {
                    LogHelper.d(USER_SOURCE_TAG, "用户来源:--Facebook买量");
                }
                return USER_FROM_FACEBOOK_PURCHASE;
            }
            if (CandySaverSharedPrefManager.notOrganicUser(context)) {
                if (CandySaverSharedPrefManager.isGuideUser(context)) {
                    if (FeatureConfig.DEBUG_LOG) {
                        LogHelper.d(USER_SOURCE_TAG, "用户来源:--带量");
                    }
                    return USER_FROM_RECOMMEND;
                } else {
                    if (FeatureConfig.DEBUG_LOG) {
                        LogHelper.d(USER_SOURCE_TAG, "用户来源:--其它网盟买量");
                    }
                    return USER_FROM_OTHER_WEBUNION_PURCHASE;
                }
            } else {
                if (FeatureConfig.DEBUG_LOG) {
                    LogHelper.d(USER_SOURCE_TAG, "用户来源:--organic");
                }
                return USER_FROM_ORGANIC;
            }
        }
        if (FeatureConfig.DEBUG_LOG) {
            throw new RuntimeException("Unknown user source!");
        }
        return -1;
    }

    public static boolean isOrganic(Context context) {
        return USER_FROM_ORGANIC == getUserSource(context);
    }

    //根据指定的时间戳判断当前用户是否为新用户
    public static boolean isNewUserByTimeStamp(long timeStamp) {
        return getFirstActiveTime(CleanApplication.getInstance()) > timeStamp;
    }
}
